﻿/// <reference path="ppds.js" />
/// <reference path="AjaxSuccess.js" />


var storyBoradTabClickAction;


// only open a new tab for storiboard and activate the newly created tab
function OpenStoryBoard(action) {

    myLayout.close('east');
    var count = $(tab_id).tabs('length');

    if (count < 2) {
        $(tab_id).tabs("add", "#tabs-1", "StoryBoard");
    }


    storyBoradTabClickAction = action;

    SetToOrDefaultTab(1);

    storyBoard = new StoryBoard();
    storyBoard.SelectedArticle = new Article();



}


function LoadStoryBoard(action)
{

   // AjaxCall(action, tab_1_id);
    AjaxCallWithSuccess(action, jQuery.StoryboardOnSuccess, tab_1_id);
    
}


var currentlyDraggingItem;

var storyBoard;

// StoryBorad class
function StoryBoard() {
    this.PublicationId = 0;
    this.SelectedArticle = new Object();
    this.CurrentPageNumber = 0;
    this.TargetPageNumber = 0;



}


function Article() {
    this.Id = 0;
    this.left = 0;
    this.top = 0;
}



function MoveArticle() {
    var articleId = storyBoard.SelectedArticle.Id.split('-'); // split needed for removing 'article-' part from article div id
    var targetPageNumber = storyBoard.TargetPageNumber.split('-'); // split needed for removing 'pageNumber-' part from article div id
    var scrolTop = document.getElementById('StoryBoard').scrollTop;        
    
    //alert(articleId[1]);
  //  alert(targetPageNumber[1]);

    if (storyBoard.TargetPageNumber == storyBoard.CurrentPageNumber) {
        UndoArticleMove()
        return;
    }

    var action = 'Article/MoveArticle';
    var data = 'articleId=' + articleId[1] + '&newStartPage=' + targetPageNumber[1];

    var target = tab_1_id;
    $.ajax({
        type: 'POST',
        url: jQuery.CreateAjaxUrl(action),
        data: data,
        success: function (data, textStatus, XMLHttpRequest) {
            var ajaxResponse = new jQuery.AjaxResponse(XMLHttpRequest);
            if (ajaxResponse.IsContentJSON) {
                //alert(XMLHttpRequest.responseText) ;
                UndoArticleMove();
                return;
            }
            else if (ajaxResponse.IsContentHtml) {

                $(target).html(ajaxResponse.Content);
                document.getElementById('StoryBoard').scrollTop = scrolTop;
                groupArticle();

            }
        }
    });






    }

function UndoArticleMove()
{
    //$(storyBoard.SelectedArticle.Id).css('left', storyBoard.SelectedArticle.left);
    // $(storyBoard.SelectedArticle.Id).css('top', storyBoard.SelectedArticle.top);
    $('#' + storyBoard.SelectedArticle.Id).css('left', 0);
    $('#' + storyBoard.SelectedArticle.Id).css('top', 0);

}


function TabTest()
 {

  
     $('#tabs ul li').each(
     function (index) {

         var  a = $(this).find('a').find('span').html();
         alert(a);

     }
     );

 }




 function groupArticle() {

     $('#StoryBoard').find('.pageGroup').each(
     function () {


         if ($(this).find('.Page').size() == 1) // first group or last group 
         {

             if (IsFullPageArticle(this) == true) {
                 $(this).find('.Page').addClass('fullPageBorder');
             }


         }
         else {
             if (IsGroupPageArticle(this) == true) {
                 $(this).addClass('pageGroupBorder');
             }
             else {
                 AddBorderForSinglepageArticle(this);

             }


         }

         AddClickEventForEmptyPage(this); // clicking on a empty page will open article tab in new mode with the page number selected

     }
       );


     WrapPartialArticle();
 }




       function IsFullPageArticle(pageGroup) {
           var returnValue = false;
           $(pageGroup).find('.Page').each(

                   function () {

                       if ($(this).find('.article-region').size() > 0) // check if there any artilce exist under this page
                       {
                           if ($(this).find('.ThreeQuarter').size() == 0 && $(this).find('.half').size() == 0 && $(this).find('.quarter').size() == 0) {

                               returnValue = true;
                               return false; // break the foreach loop
                           }
                           else {

                               return false; // break the each loop
                           }

                       }
                       else {
                           
                            
                           return false; 
                        }
                   }

           );

                   return returnValue;

               }




               function IsGroupPageArticle(pageGroup) {
                   var returnValue = false;
                   $(pageGroup).find('.Page').each(

                   function () {

                       if ($(this).find('.article-region').size() > 0) // check if there any artilce exist under this page
                       {

                           if ($(this).find('.ThreeQuarter').size() == 0 && $(this).find('.half').size() == 0 && $(this).find('.quarter').size() == 0) {

                               returnValue = true;

                           }
                           else {
                               returnValue = false;
                               return false; // break the each loop
                           }

                       }
                       else {

                           returnValue = false;
                           return false;
                       }
                   }

           );


                   if (returnValue == true) {

                       var page1ArticleId = GetArticleIdByDivId($(pageGroup).find('.Page').first().find('.article-region').first().attr('id'));
                       var page2ArticleId = GetArticleIdByDivId($(pageGroup).find('.Page').last().find('.article-region').first().attr('id'));

                       if (page1ArticleId == page2ArticleId) {
                           returnValue = true;
                       }
                       else
                       {returnValue = false;}

                   }
                   return returnValue;

               }



               function AddBorderForSinglepageArticle(pageGroup) 
               {
                   if ($(pageGroup).find('.Page').first().find('.article-region').size() == 1) 
                   {
                       if ($(pageGroup).find('.Page').first().find('.ThreeQuarter').size() == 0 && $(pageGroup).find('.Page').first().find('.half').size() == 0 && $(pageGroup).find('.Page').first().find('.quarter').size() == 0) {

                           $(pageGroup).find('.Page').first().addClass('fullPageBorder');
                     

                       }

                   }

                   if ($(pageGroup).find('.Page').last().find('.article-region').size() == 1)
                    {
                        if ($(pageGroup).find('.Page').last().find('.ThreeQuarter').size() == 0 && $(pageGroup).find('.Page').last().find('.half').size() == 0 && $(pageGroup).find('.Page').last().find('.quarter').size() == 0) {

                            $(pageGroup).find('.Page').last().addClass('fullPageBorder');
                           
                        }


                    }

                    return;

                }


                function WrapPartialArticle()
                {
                    $('#StoryBoard').find('.pageGroup').find('.Page').find('.ThreeQuarter').wrap('<div class="ThreeQuarterArticleWapper articleWrapper " />');
                    $('#StoryBoard').find('.pageGroup').find('.Page').find('.half').wrap('<div class="halfArticleWapper articleWrapper " />');
                    $('#StoryBoard').find('.pageGroup').find('.Page').find('.quarter').wrap('<div class="quarterArticleWapper articleWrapper " />');
                    //$('#StoryBoard').find('.pageGroup').find('.Page').find('.half,.ThreeQuarter,.quarter').wrap('<div class="partialArticleWapper" />');
                   // AdjastPartialArticlePosition();


                }


                function AddClickEventForEmptyPage(pageGroup) {
                    var publicationId = $('#publicationId').val();

                    $(pageGroup).find('.Page').each(
                        function () {
                            if ($(this).find('.article-region').size() == 0) {

                                var pageNumber = GetActualIdByDivId($(this).attr('id'))
                                

                                $(this).bind('click', function () { OpenArticle('/Article/index?publicationId='+publicationId+'&articleId=0&startPage=' + pageNumber); });
                            }
                        }
                    );

                }


                function GetArticleIdByDivId(articleDivid) {
                    var start = articleDivid.indexOf('-');
                    var end = articleDivid.toString().length;

                    var articleId = articleDivid.substring(start, end);
                    return articleId;

                }

                function GetActualIdByDivId(Divid) {
                    var start = Divid.indexOf('-') + 1;
                    var end = Divid.toString().length;

                    var actualId = Divid.substring(start, end);
                    return actualId;

                }


                // function added for future use
                function AdjastPartialArticlePosition()
                {
                    var css;
                    var firstWrapper;
                    $('#StoryBoard').find('.pageGroup').find('.Page').each(
                    function () {

                        if ($(this).find('.articleWrapper').length>0) {

                       
                        firstWrapper = $(this).find('.articleWrapper').first();
                        if (firstWrapper)
                         {
                             css = GetPartialArticlePositionCSS(firstWrapper);
                             $(firstWrapper).addClass(css);
                        }
                     }
                    }

                    );
                    

                    
                    


                   
                }


                function GetPartialArticlePositionCSS(wrapper)
                 {
                    var css;
                    var startPage = $(wrapper).find('label:first').attr('id');

                    if (startPage == 0) {
                        css = 'position1';
                    }
                    else if (startPage == 0.25) 
                    {
                        css = 'position2';
                    }
                    else if(startPage == 0.50)
                    {
                        css = 'position3';
                    }

                    else if (startPage == 0.75) {
                        css = 'position4';
                    }

                    return css;
 

                }